package network

import (
	"fmt"
	"log"
	"network/ip"
	"network/ip/icmp"
	"network/tun"
)

func NetWork() {
	fd, err := tun.OpenTUN("tun0")
	if err != nil {
		return
	}
	for {
		buf, err := tun.Read(fd)
		if err != nil {
			log.Fatal(err.Error())
		}
		ipv4, err := ip.NewIPV4(buf)
		if err != nil {
			fmt.Println(err)
			continue
		}
		if ipv4.Header.Protocol() != ip.ICMPProtocol {
			continue
		}
		icmpS := icmp.ICMPv4(ipv4.Data)
		if icmpS.Type() != icmp.PingType {
			continue
		}

		// 校验和
		echo, err := icmp.NewEcho(ipv4.Data)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(echo)
	}
}
